package com.horizon.common.repository;

import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.NonNull;
import lombok.Setter;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * HQL封装基类
 *
 * @author wangfuwei
 * @version 2018-08-07
 */
@NoArgsConstructor
@Getter
@Setter
public class HQLInfo {

    public HQLInfo(String modelName) {
        this.modelName = modelName;
    }

    /**
     * 实体类类名
     */
    @NonNull
    private String modelName;

    /**
     * 查询表名
     */
    private String tableName;

    /**
     * 排序
     */
    private String orderBy;

    /**
     * 当前页码
     */
    private Integer pageNo = 1;

    /**
     * 每页条数
     */
    private Integer rowSize = 10;

    private String selectBlock;

    private String fromBlock;

    private String joinBlock;

    private String whereBlock;

    /**
     * 查询参数
     */
    private Map<String, Object> parameterMap = new LinkedHashMap<>();

    public void setParameter(String key, Object value) {
        this.parameterMap.put(key, value);
    }

    /**
     * 解析为HQL语句
     */
    String buildHQL(Boolean isCount) {
        StringBuilder hql = new StringBuilder();
        if (this.getTableName() == null || this.getTableName().isEmpty()) {
            this.setTableName(this.getModelName());
            int i = this.getTableName().lastIndexOf('.');
            if (i > -1) this.setTableName(this.getTableName().substring(i + 1));
            this.setTableName(this.getTableName().substring(0, 1).toLowerCase() + this.getTableName().substring(1));
        }
        if (isCount) {
            hql.append("select count(*) ");
            if (this.getFromBlock() == null || this.getFromBlock().isEmpty()) {
                hql.append("from ").append(this.getModelName()).append(" ").append(this.getTableName()).append(" ");
            } else {
                hql.append("from ").append(this.getFromBlock()).append(" ");
            }
        } else {
            if (this.getSelectBlock() == null || this.getSelectBlock().isEmpty()) {
                hql.append("select ").append(this.getTableName()).append(" ");
            } else {
                hql.append("select ").append(this.getSelectBlock()).append(" ");
            }

            if (this.getFromBlock() == null || this.getFromBlock().isEmpty()) {
                hql.append("from ").append(this.getModelName()).append(" ").append(this.getTableName()).append(" ");
            } else {
                hql.append("from ").append(this.getFromBlock()).append(" ");
            }
        }

        if (this.getJoinBlock() != null && !this.getJoinBlock().isEmpty()) {
            hql.append(this.getJoinBlock()).append(" ");
        }

        if (this.getWhereBlock() != null && !this.getWhereBlock().isEmpty()) {
            hql.append("where ").append(this.getWhereBlock()).append(" ");
        }

        if (!isCount && this.getOrderBy() != null && !this.getOrderBy().isEmpty()) {
            hql.append("order by ").append(this.getOrderBy());
        }

        return hql.toString();
    }
}